package CAT200;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JFileChooser;

/*

package CAT200;

/**
 *
 * @author jocelynn
 */
public class Add_New_Item extends javax.swing.JFrame {

	int sizeCount, colorCount;
	String sizeCombo, colorCombo;
	
    /** Creates new form Add_New_Item2 */
    public Add_New_Item() {
        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        sizeCount = 0;
        colorCount = 0;
        Calendar cal = new GregorianCalendar();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        //System.out.println("Current date : " + day + "/" + (month + 1) + "/" + year);
        String m = Integer.toString(month+1);
        String y = Integer.toString(year);
        String d = Integer.toString(day);
        dateLabel.setText(year+"-"+(month+1)+"-"+day);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        categoryBox = new javax.swing.JComboBox();
        sizeBox = new javax.swing.JComboBox();
        colorBox = new javax.swing.JComboBox();
        quantityLabel = new javax.swing.JTextField();
        priceLabel = new javax.swing.JTextField();
        descriptionLabel = new javax.swing.JTextField();
        pictureTextfield = new javax.swing.JTextField();
        BrowseButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        AddButton = new javax.swing.JButton();
        AddAnotherButton = new javax.swing.JButton();
        codeLabel = new javax.swing.JLabel();
        dateLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add New Item", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12), new java.awt.Color(53, 57, 58))); // NOI18N

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel1.setText("Category :");

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel2.setText("CODE :");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel3.setText("Description :");
        
        descriptionLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); // NOI18N
        descriptionLabel.setText(" ");
        descriptionLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel4.setText("Quantity :");
        
        quantityLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); // NOI18N
        quantityLabel.setText(" ");
        quantityLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel5.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); 
        jLabel5.setText("Price :");

        priceLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); // NOI18N
        priceLabel.setText(" ");
        priceLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        
        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel6.setText("Date :");

        jLabel7.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); 
        jLabel7.setText("Size :");

        jLabel8.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); 
        jLabel8.setText("Colour :");

        jLabel9.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); 
        jLabel9.setText("Picture :");
        
        pictureTextfield.setText(" ");

        categoryBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose One", "CLT", "PNT", "SKT" }));
        categoryBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                categoryBoxActionPerformed(evt);
            }
        });

        sizeBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose One", "S", "M", "L", "XL", "XXL" }));
        sizeBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
              //  sizeBoxActionPerformed(evt);
            }
        });

        colorBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose One", "BLACK", "WHITE", "BLUE", "RED", "YELLOW" }));
        colorBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
               // colorBoxActionPerformed(evt);
            }
        });

        BrowseButton.setText("Browse");
        BrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BrowseButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        AddButton.setText("Add");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        AddAnotherButton.setText("Add and Add Another");
        AddAnotherButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddAnotherButtonActionPerformed(evt);
            }
        });

        codeLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); 
        codeLabel.setText("  ");
        codeLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        dateLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); 
        dateLabel.setText("   ");
        dateLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel9)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(AddAnotherButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(pictureTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BrowseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))
                    .addComponent(descriptionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(dateLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(codeLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(priceLabel, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(quantityLabel, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(colorBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(sizeBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(categoryBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(categoryBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(codeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(descriptionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(quantityLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(priceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(dateLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(sizeBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(colorBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(pictureTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BrowseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelButton)
                    .addComponent(AddButton)
                    .addComponent(AddAnotherButton))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void BrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        JFileChooser filechooser = new JFileChooser();

        if(filechooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
        {
            String address = filechooser.getSelectedFile().getPath();
            System.out.println("Address : "+ address);
            pictureTextfield.setText(address);
            
        }
    }
    
    private void AddAnotherButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	addItem();
        this.setVisible(false);
        this.dispose();
        new Add_New_Item().setVisible(true);
    }

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	addItem();
        this.setVisible(false);
        this.dispose();
        new AdminMenu().setVisible(true);

    }

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	this.setVisible(false);
    	this.dispose();
    	new AdminMenu().setVisible(true);
    }
    
    private void categoryBoxActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	try
    	{
    	DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/CAT200-DB", "user", "123");
        
        Statement stmt = con.createStatement();
        
        ResultSet result = stmt.executeQuery("SELECT COUNT(PRODUCT_CODE) FROM GALLERY WHERE PRODUCT_CODE LIKE '"+categoryBox.getSelectedItem()+"%' ");
        
        int count =0;
        while(result.next())
        {
        	//System.out.println(result.getInt(1));
             count = result.getInt(1);
            count = 1000+count;     
        }
        String temp = Integer.toString(count);
        codeLabel.setText(temp);

        con.close();
        stmt.close();

    	}
    	catch(Exception e)
    	{  
    	System.out.println("Count data error: "+e);	
    	}
    }

    
    private void addItem()
    {  	        
        String P_code = (categoryBox.getSelectedItem()).toString()+codeLabel.getText();
        String P_Desciption = descriptionLabel.getText();
        String P_Quantity = quantityLabel.getText();
        String P_Price = priceLabel.getText();
        String P_Date = dateLabel.getText();
        String P_Location = pictureTextfield.getText();
        Object a =colorBox.getSelectedItem();
        String b = a.toString();
        Object c =sizeBox.getSelectedItem();
        String d = c.toString();
        
        String P_Size = sizeCombo;
        String P_Colour = colorCombo;
        
    	try
    	{
    	DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/CAT200-DB", "user", "123");
        
        Statement stmt = con.createStatement();

        String Query = "INSERT INTO GALLERY VALUES ('"+P_code+"','"+P_Desciption+"',"+P_Quantity+","+P_Price+",'"+P_Date+"','"+b+"','"+d+"','"+P_Location+"')";

        stmt.executeUpdate(Query);



        con.close();
        stmt.close();

        

        
    	}
    	catch(Exception e)
    	{  
    	System.out.println("Insert data error: "+e);	
    	}
    }

    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Add_New_Item().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton AddAnotherButton;
    private javax.swing.JButton AddButton;
    private javax.swing.JButton BrowseButton;
    private javax.swing.JButton CancelButton;
    private javax.swing.JComboBox categoryBox;
    private javax.swing.JLabel codeLabel;
    private javax.swing.JComboBox colorBox;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JTextField descriptionLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField pictureTextfield;
    private javax.swing.JTextField priceLabel;
    private javax.swing.JTextField quantityLabel;
    private javax.swing.JComboBox sizeBox;
    // End of variables declaration

}
